<template>
    <div style="width:500px;margin: 0 auto;">
        <h3 style="text-align: center; margin: 20px 0;">您正在向{{masterName}}发起提问</h3>
        <div style="border: 1px solid #E1E1E1;">
            <div style="border-bottom: 1px solid #E1E1E1;">
                <Input v-model="title" type="text" placeholder="请输入标题"></Input>
            </div>
            <div style="border-bottom: 1px solid #E1E1E1;">
                <Input v-model="questionDesc" type="textarea" :autosize="{minRows: 10}" :maxlength="150"
                       placeholder="请输入您的问题描述，提问的问答每被偷窥1次，你分成0.5元"></Input>
            </div>
            <div style="padding: 15px; line-height: 30px; font-size: 16px;">
                <div style=" margin: 10px 0;">
                    <span>¥10</span>
                    <span style="margin-left: 140px;">
                        <Icon type="image"></Icon>
                        <Icon type="android-add"></Icon>
                    </span>
                    <span style="float:right;">{{textLenth}}/150</span>
                </div>
                <div style=" margin: 10px 0;cursor:pointer;">
                    <span>提问须知</span>
                    <Icon type="chevron-right" style="float: right; margin-right: 10px;"></Icon>
                </div>
                <div style="padding: 10px 0; text-align: center;">
                    <Button type="primary" @click="handleSubmit">提问</Button>
                </div>
            </div>
        </div>
    </div>
</template>
<script type="text/ecmascript-6">

    import getData from '../../../api/getData'

    export default {
        data: function () {
            return {
                AID: '',
                masterId: '',
                masterName: '',
                title: '',
                questionDesc: '',
                url: '',
                cost: 10
            }
        },
        computed: {
            textLenth(){
                return this.questionDesc.length;
            }
        },
        created(){
            var parameterString = location.href.split('?')[1];
            var parameterList = parameterString.split('&');
            parameterList[0] = parameterList[0].substring(parameterList[0].indexOf('?') + 1, parameterList[0].length)
            var temp_key = null;
            var result_obj = {};
            for (var i = 0; i < parameterList.length; i++) {
                temp_key = parameterList[i].substring(0, parameterList[i].indexOf('='));
                result_obj[temp_key] = decodeURIComponent( parameterList[i].substring(parameterList[i].indexOf('=') + 1, parameterList[i].length) );
            }
            this.masterId = result_obj.id;
            this.masterName = result_obj.name;
            this.AID = sessionStorage.getItem('YZ_AID');
        },
        methods: {
            handleSubmit(){
                if(this.questionDesc && this.title){
                    this.updateAccountABalance();
                }else{
                    this.$Message.info( !this.title ?'标题不能为空' : '请输入问题描述');
                }
            },

            updateAccountABalance(){
                var self = this;
                getData.post("updateAccountABalance", {
                    AID: self.AID,
                    ABalance: self.cost
                }).then(function (response) {
                    if (response.data.code == 0) {
                        self.submitQustion();
                    } else {
                        self.$Message.info(response.data.msg ? response.data.msg : '支付失败');
                    }

                }).catch(function (response) {
                    self.$Message.info('网络错误，请联系客服');
                });
            },

            submitQustion(){
                var self = this;
                var param = {
                    QaMasterID: self.masterId,
                    AID: self.AID,
                    QaClassificationID: '',
                    QaQTitle: '',
                    QaQProblem: self.questionDesc,
                    QaQPicture: self.url,
                    QaQMoney: self.cost
                };
                getData.post("submitQustion", {
                    StrQAQuestions: JSON.stringify(param)
                }).then(function (response) {
                    if (response.data.code == 0) {
                        var tempData = response.data.data;
                        self.qaMasterList = tempData;
                    } else {
                        self.$Message.info(response.data.msg ? response.data.msg : '网络错误，请稍后重试');
                    }

                }).catch(function (response) {
                    self.$Message.info('网络错误，请联系客服');
                });
            }

        }
    }
</script>
<style lang="scss">
</style>
